package com.kaishengit.demo.thread;

public class EnergyTransferTask implements Runnable {

    private EnergySystem energySystem;
    private int fromBox;
    private int maxAmount;
    private int delay = 10;

    public EnergyTransferTask(EnergySystem energySystem,int from,int amount){
        this.energySystem = energySystem;
        this.fromBox = from;
        this.maxAmount = amount;
    }

    @Override
    public void run() {

        for (int i=0;i<10;i++){
            int toBox = (int) (energySystem.getBoxAmount()*Math.random());
            int amount = (int) (maxAmount*Math.random());
            energySystem.transfer(fromBox,toBox,amount);
            try {
                Thread.sleep((long) (delay*Math.random()));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
